/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Core.Model.Data.Variables;
using Core.Model.Events;
using Core.Model.Data.Generics;
using Core.Model.Data.Type;

namespace Core.Model
{
    public class Bitmap : Visible
    {        
        #region   Exchanged Attributes
        private bool tiling = false;

        public bool Tiling
        {
            get { return tiling; }
            set { tiling = value; }
        }
        private int originalTransparency = 0;

        public int OriginalTransparency
        {
            get { return originalTransparency; }
            set { originalTransparency = value; }
        }

        private Size? scale = null;

        public Size? Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        #endregion

        #region  Internal Attributes
        private int transparency;

        public int Transparency
        {
            get { return transparency; }
            set { transparency = value; }
        }
        private Point decodeOffset = new Point( 0, 0 );

        public Point DecodeOffset
        {
            get { return decodeOffset; }
            set { decodeOffset = value; }
        }

        #endregion

        #region  Actions
        public void ScaleBitmap ( GenericInteger xScale, GenericInteger yScale )
        {
            Scale = new Size( xScale.Value, yScale.Value );

            if ( RunningStatus )
            {
                OnRedrawArea( BoxPosition, BoxSize );
            }
        }

        public void GetBitmapDecodeOffset ( IntegerVariable x, IntegerVariable y )
        {
            x.Value = decodeOffset.X;
            y.Value = decodeOffset.Y;
        }

        public void SetBitmapDecodeOffset ( GenericInteger x, GenericInteger y )
        {
            decodeOffset.X = x.Value;
            decodeOffset.Y = y.Value;
            if ( RunningStatus )
            {
                OnRedraw();
            }
        }

        public void SetTransparency ( GenericInteger newTransparency )
        {
            this.transparency = newTransparency.Value;

            if ( RunningStatus )
            {
                OnRedrawArea( BoxPosition, BoxSize );
            }
        }

        public override void InittInternalAttributes ()
        {
            ContentHook = ContentHook ?? MHEGEngine.Instance.ActiveApplication.BitmapContentHook;

            base.InittInternalAttributes();

            transparency = originalTransparency;
        }

        public override void SetData ( Core.Model.Data.Generics.GenericContentRef newContent )
        {
            if ( newContent == null )
            {
                log.Error( "SetData: new included content is null." );
                return;
            }
 
            base.SetData( newContent );
            MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );

        }

        public override void SetData ( Core.Model.Data.Generics.GenericOctetString newContent )
        {
            if ( newContent == null )
            {
                log.Error( "SetData: new included content is null." );
                return;
            }

            base.SetData( newContent );

            log.Warn( "SetData: Trying to set bitmap with included data ("+newContent.Value+"). Trying to use as reference" );
            SetData( new GenericContentRef( new ContentRef( newContent.Value ) ) );
 
            MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );
        }

        #endregion

    }
}
